import { fileURLToPath, URL } from 'node:url'
const fs = require('fs')
const moment = require('moment')

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import path from 'path'

const childProcess = require('child_process')
const version = childProcess
  .execSync('git rev-parse --abbrev-ref HEAD')
  .toString()
  .replace(/\s+/, '')
  .split('-')
  .pop()
  .split('/')
  .pop()

const buildTime = `${moment(new Date()).format('YYYYMMDD-HHmmss')}`
process.env.VITE_BUILD_VERSION = version
process.env.VITE_BUILD_TIME = buildTime

fs.writeFile(path.resolve(`./public/buildTime.txt`), buildTime, (err: any) => {
  console.log(err ?? 'build标记成功')
})
const { resolve } = require('path')
const isProd = process.env.VITE_ENV == 'prod'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue(), vueJsx()],
  esbuild: {
    drop: isProd ? ['console', 'debugger'] : []
  },
  css: {
    // 预处理器配置项
    preprocessorOptions: {
      less: {
        javascriptEnabled: true
      }
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
      __com: resolve('./node_modules/@dt/pc/dist/com'),
      __store: resolve('./node_modules/@dt/pc/dist/store'),
      __config: resolve('./node_modules/@dt/pc/dist/config')
    },
    extensions: ['.js', '.ts', '.vue']
  }
})
